const path = require('path');

const buble = require('rollup-plugin-buble')

const alias = require('rollup-plugin-alias');

const flow = require('rollup-plugin-flow-no-whitespace');

const node = require('rollup-plugin-node-resolve');

const commonjs = require('rollup-plugin-commonjs')

const resolve = p => path.resolve(__dirname, '../', p);

const babel = require('rollup-plugin-babel');

const { uglify } = require('rollup-plugin-uglify'); //压缩文件

const version = process.env.VERSION || require('../package.json').version

const banner =
  '/*!\n' +
  ` * Rollup.js v${version}\n` +
  ` * (c) 2021-${new Date().getFullYear()} Chen You\n` +
  ' * Released under the MIT License.\n' +
  ' */'

const builds = {
    'web-full-dev': {
        entry: resolve('src/entry.js'),
        dest: resolve('dist/rollup.js'),
        format: 'umd', // 指定输出格式  五种输出格式：amd /  es6 / iife / umd / cjs
        env: 'development',
        banner, // 在打包好的文件的块的外部(wrapper外部)的最顶部插入一段内容
        
    },
    'web-full-pro': {
        entry: resolve('src/entry.js'),
        dest: resolve('dist/rollup.min.js'),
        format: 'umd',
        env: 'production',
        plugins: [ uglify() ],
        sourceMap: true,
    },
};

const genConfig = (name) => {
    const opts = builds[name];
    const config = {
        input: opts.entry,
        external: opts.external,
        plugins: [
            flow(),
            node(),
            commonjs(),
            buble(),
            babel({
                extensions: [ '.js' ],
                runtimeHelpers: true,
                exclude: ["node_modules/**"]
            })
        ].concat(opts.plugins || []),
        output: {
            file: opts.dest,
            format: opts.format,
            banner: opts.banner,
            name: opts.moduleName || 'StopSystem',
            sourcemap: opts.env === 'production'
        },
    };
    console.log(config);
    return config;
};

if (process.env.TARGET) {
    module.exports = genConfig(process.env.TARGET)
}